package com.gxks.lhs.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.Constants;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.User;
import com.gxks.lhs.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-17
 */
@Api(tags = "用户管理模块")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("封禁用户，把用户的状态改为禁用状态")
    @GetMapping("/ban/{id}")
    public R banUser(@PathVariable(value = "id", required = true) String id) {
        boolean b = userService.banUser(id);
        if (b) {
            return R.success("封禁成功");
        }
        return R.error("封禁失败");
    }

    @ApiOperation("解封用户，把用户的状态改为正常d状态")
    @GetMapping("/unban/{id}")
    public R unbanUser(@PathVariable(value = "id", required = true) Integer id) {
        boolean b = userService.unseal(id);
        if (b) {
            return R.success("解封成功");
        }
        return R.error("解封失败");
    }

    @ApiOperation("修改用户信息")
    @PutMapping
    public R updateUser(@RequestBody com.gxks.lhs.entity.User user) {
        boolean b = userService.updateUser(user);
        if (b) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @ApiOperation("查询所有用户信息")
    @GetMapping("/list")
    public R listUser(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                      @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<User> page = userService.findAllUsers(current, size);
        return R.success(page, "查询成功");
    }

    @ApiOperation("根据条件查询用户列表")
    @PostMapping("/list")
    public R listUser(@RequestBody User user) {
        Page<User> page = userService.findUsersByCondition(user, Constants.CURRENT_PAGE, Constants.PAGE_SIZE);
        return R.success(page, "查询成功");
    }

    @ApiOperation("根据id查询用户信息")
    @GetMapping("/{id}")
    public R getUserById(@PathVariable(value = "id", required = true) String id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return R.success(user, "查询成功");
        }
        return R.error("查询失败");
    }
}
